package me.zhengjie.modules.insurance.rest;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.insurance.pojo.*;
import me.zhengjie.modules.insurance.service.InsuredApplyService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 用户申请案件API
 *
 * @author HeHui
 * @date 2022-01-12 23:43
 */
@RequestMapping("/api/insured/user-apply")
@RestController
@Api("用户申请案件API")
@RequiredArgsConstructor
public class InsuredUserApplyController {

    private final InsuredApplyService insuredApplyService;


    /**
     * 匹配 最后一个ID
     *
     * @param mobile 移动
     * @param time   时间
     * @return {@link ResponseEntity}<{@link Long}>
     */
    @GetMapping("/matching")
    @AnonymousAccess
    @ApiOperation("根据手机号码和时间戳匹配一个案件")
    public ResponseEntity<InsuredApplyRequest> matching(@RequestParam String mobile, @RequestParam Long time) {
        Date basic = new Date(time);
        return new ResponseEntity<>(insuredApplyService.mobileMatching(mobile, DateUtil.beginOfDay(basic), DateUtil.endOfDay(basic)), HttpStatus.OK);
    }


    /**
     * 申请
     *
     * @param request 请求
     * @return {@link ResponseEntity}<{@link Long}>
     */
    @PostMapping
    @AnonymousAccess
    @ApiOperation("案件申请")
    public ResponseEntity<Long> apply(@RequestBody @Validated InsuredUserApplyRequest request) {
        return new ResponseEntity<>(insuredApplyService.userApply(request),HttpStatus.OK);
    }



    @PutMapping
    @AnonymousAccess
    @ApiOperation("案件二次编辑")
    public ResponseEntity<Long> edit(@RequestBody @Validated InsuredUserUpdateRequest request) {
        insuredApplyService.userEdit(request);
        return new ResponseEntity<>(request.getId(),HttpStatus.OK);
    }



    @PostMapping("/replenish")
    @AnonymousAccess
    @ApiOperation("补充资料")
    public ResponseEntity<Void> replenishDoc(@RequestBody @Validated UserCaseReplenishRequest request) {
        insuredApplyService.replenishDoc(request.getDocs(), request.getId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/speed")
    @AnonymousAccess
    @ApiOperation("查看案件进度")
    public ResponseEntity<InsuredCaseSpeedResponse> speed(@RequestParam Long id) {
        return new ResponseEntity<>(insuredApplyService.getSpeed(id),HttpStatus.OK);
    }

}
